package cn.waka.sys.admin.controller;

import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Menu;
import cn.waka.sys.admin.object.Node;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.service.MenuService;
import cn.waka.sys.admin.service.NodeService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private NodeService nodeService;

    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }


    public void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }


    @RequestMapping(path = "/index", name = "菜单列表", produces = "text/html; charset=UTF-8")
    public ModelAndView menuList(HttpServletRequest req, HttpServletResponse res) {
        Collection<Menu> menuList = menuService.findMenus();
        ModelAndView mv = new ModelAndView();
        mv.addObject("menus", menuList);
        return mv;
    }

    @RequestMapping(path = "/form", name = "修改菜单", produces = "text/html; charset=UTF-8")
    public ModelAndView form(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        Menu menu = new Menu();
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
            menu = menuService.findMenuById(id);
        }
        List<Menu> menuList = menuService.selectedMenus();
        List<Node> nodeList = nodeService.getSelectNode();
        ModelAndView mv = new ModelAndView();
        mv.addObject("menus", menuList);
        mv.addObject("nodes", nodeList);
        mv.addObject("menu", menu);
        return mv;
    }

    @RequestMapping(path = "/add", name = "添加菜单", produces = "text/html; charset=UTF-8")
    public ModelAndView add(HttpServletRequest req, HttpServletResponse res) {
        List<Menu> menuList = menuService.findMenus();
        List<Node> nodeList = nodeService.getSelectNode();
        ModelAndView mv = new ModelAndView();
        mv.addObject("menus", menuList);
        mv.addObject("nodes", nodeList);
        return mv;
    }

    @RequestMapping(path = "/icon", name = "选择图标", produces = "text/html; charset=UTF-8")
    public ModelAndView selectResource() {
        return new ModelAndView("/menu/icon");
    }

    @RequestMapping(path = "/save", name = "保存菜单", produces = "application/json; charset=UTF-8")
    public Result save(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String pidStr = req.getParameter("pid");
        int pid = 0;
        if (pidStr != null && Validator.isNum(pidStr)) {
            pid = Integer.parseInt(pidStr);
        }
        String nodeIdStr = req.getParameter("nodeId");
        int nodeId = 0;
        if (nodeIdStr != null && Validator.isNum(nodeIdStr)) {
            nodeId = Integer.parseInt(nodeIdStr);
        }
        String title = req.getParameter("title");
        String url = req.getParameter("url");
        String icon = req.getParameter("icon");
        Menu menu = new Menu();
        menu.setId(id);
        menu.setPid(pid);
        menu.setNodeId(nodeId);
        menu.setTitle(title);
        menu.setUrl(url);
        menu.setIcon(icon);
        int rs = menuService.save(menu);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/updateStatus", name = "菜单启用|禁用", produces = "application/json; charset=UTF-8")
    public Result updateStatus(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String statusStr = req.getParameter("status");
        int status = 0;
        if (statusStr != null && Validator.isNum(statusStr)) {
            status = Integer.parseInt(statusStr);
        }
        int rs = menuService.updateStatus(status, id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/updateSort", name = "菜单排序", produces = "application/json; charset=UTF-8")
    public Result updateSort(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("name");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String sortStr = req.getParameter("value");
        int sort = 0;
        if (sortStr != null && Validator.isNum(sortStr)) {
            sort = Integer.parseInt(sortStr);
        }
        int rs = menuService.updateSort(sort, id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/del", name = "删除菜单", produces = "application/json; charset=UTF-8")
    public Result del(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = menuService.delete(id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败或无法删除！");
        }
    }

}
